/*
 * dsSignaler.cpp
 *
 *  Created on: Dec 18, 2013
 *      Author: m
 */

#include "Semaphore.h"
#include "ColaRemotaTransmisor.h"
#include "CPrinter.h"

#define DS_SEM_BASEFILE_ARG 1
#define DS_SEM_KEY_ARG 2
#define DS_REMOTEQUEUE_ARG 3

using ipc::Semaphore;
using ipc::CPrinter;

int main ( int argc, char* argv[] )
{
    std::string ipcBaseFile;
    char ipcKey;
    std::string remoteQueueName;

    CPrinter::setHeader( "dsSignaler" );

    if ( argc != 4 )
    {
        CPrinter::printErr( "ERROR: Cantidad de argumentos invalida" );
        CPrinter::printErr( "USO: dsSignaler [SEM_BASEFILE] [SEM_KEY] [DSEM_NAME]" );

        exit(1);
    }

    ipcBaseFile = argv[DS_SEM_BASEFILE_ARG];
    ipcKey = argv[DS_SEM_KEY_ARG][0];
//    ipcKey = StringUtils::StringToNumber<int>( argv[DS_SEM_KEY_ARG] );
    remoteQueueName = argv[DS_REMOTEQUEUE_ARG];

    CPrinter::setHeader( "dsSignaler " + remoteQueueName );

    Semaphore sem = Semaphore::get( ipcBaseFile, ipcKey );
    ColaRemotaTransmisor trans;

    CPrinter::printOut( "INICIADO" );

    CPrinter::printOut( "Procesando Signals del sem: (%s,%d)", ipcBaseFile.c_str(), ipcKey );

    while ( true )
    {
        // Espero sobre el semaforo
        sem.p();

        CPrinter::printOut( "Enviando SIGNAL a: %s (%s,%d)", remoteQueueName.c_str(), ipcBaseFile.c_str(), ipcKey );

        // mando un paquete a la cola del dsWaiter
        trans.enviarMensaje<int>( remoteQueueName, 1 );
    }

    return 0;
}
